//
//  MsgBaseCell.swift
//  swiftim
//
//  Created by yangjian on 2023/6/9.
//

import Foundation
import RxSwift

class BubbleView : UIImageView {
    var rx_messageBubbleEventSubject = PublishSubject<MsgBaseCell.MsgCellEvent>()
    var bag = DisposeBag()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect = CGRectZero) {
        super.init(frame: frame)
        self.isUserInteractionEnabled = true
        
        let tapGesture = UITapGestureRecognizer()
        self.addGestureRecognizer(tapGesture)
        tapGesture.rx.event.subscribe(onNext: {[unowned self] sender in
            if sender.state == .ended {
                self.rx_messageBubbleEventSubject.onNext(.messageTap)
            }
        }).disposed(by: bag)

        let longGesture = UILongPressGestureRecognizer()
        self.addGestureRecognizer(longGesture)
        longGesture.rx.event.subscribe(onNext: {[unowned self] sender in
            if sender.state == .began {
                self.rx_messageBubbleEventSubject.onNext(.messageLongTap)
            }
        }).disposed(by: bag)
    }
}

class IMMsgTextView : UITextView,UITextViewDelegate {
    
    var rx_msgTextSelectionEventSubject = PublishSubject<(MsgBaseCell.MsgCellEvent,String)>()
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        self.backgroundColor = UIColor.clear
        self.textContainer.lineFragmentPadding = 0
        self.textContainerInset = UIEdgeInsets.zero
        self.isEditable = false
        self.isScrollEnabled = false
        self.delegate = self
//        textView.isUserInteractionEnabled = false
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return false
    }
    
    func textViewDidChangeSelection(_ textView: UITextView) {
        if self.text.isEmpty {
            self.rx_msgTextSelectionEventSubject.onNext((.textSelectEmpty,""))
        }else if self.selectedRange.length < 1 {
            self.rx_msgTextSelectionEventSubject.onNext((.textSelectEmpty,""))
        }else{
            if self.selectedRange.length == self.text.count {
                self.rx_msgTextSelectionEventSubject.onNext((.textSelectAll,self.text))
            }else{
                guard let range = self.selectedTextRange else{
                    self.rx_msgTextSelectionEventSubject.onNext((.textSelectEmpty,""))
                    return
                }
                
                guard let subText = self.text(in: range) else{
                    self.rx_msgTextSelectionEventSubject.onNext((.textSelectEmpty,""))
                    return
                }
                self.rx_msgTextSelectionEventSubject.onNext((.textSelectPortion,subText))

            }
        }
    }
}

class MsgBaseCell : UITableViewCell {
    
    enum MsgCellEvent {
        case headTap
        case headLongTap
        case headDoubleTap
        
        //消息点击
        case messageTap
        case messageLongTap

        case textSelectEmpty
        case textSelectPortion
        case textSelectAll

        case messageMenuForward
        case messageMenuDelete
        case messageMenuQuote
        case messageMenuConvertText

    }
    
    var bag = DisposeBag()
    var reuseBag = DisposeBag()
    var rx_msgEventSubject = PublishSubject<(MsgCellBaseModel,MsgBaseCell,MsgCellEvent,String)>()
    
    var model = MsgCellBaseModel(text: "null", cellType: .system)
    weak var currentTableView : UITableView?
    var currentIndexPath = IndexPath(row: 0, section: 0)

    override func prepareForReuse() {
        
        //需要将转发器进行初始化,否则会重复添加不同的数据源
//        rx_msgEventSubject = PublishSubject<(MsgCellBaseModel,MsgBaseCell,MsgCellEvent,String)>()
        
        reuseBag = DisposeBag()
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none;
        self.backgroundColor = UIColor.clear;
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //重写需要调用 need super.bind...
    func bindFromModel(_ model : MsgCellBaseModel){
        self.model = model
        model.currentCell = self
    }
    
    
    
    
}
